package org.jet.emall.rbac.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jet.emall.common.restful.result.CommonPage;
import org.jet.emall.common.restful.result.CommonResult;
import org.jet.emall.common.util.ObjectUtils;
import org.jet.emall.log.AdminOperationLog;
import org.jet.emall.log.LogType;
import org.jet.emall.rbac.annotation.ApiInformation;
import org.jet.emall.rbac.dto.AdminPrivilegeDTO;
import org.jet.emall.rbac.dto.AdminRoleDTO;
import org.jet.emall.rbac.model.NewAdminRoleModel;
import org.jet.emall.rbac.model.UpdateAdminRoleModel;
import org.jet.emall.rbac.service.CustomAdminRoleService;
import org.jet.emall.rbac.transfer.AdminPrivilegeVoTransfer;
import org.jet.emall.rbac.transfer.AdminRoleVoTransfer;
import org.jet.emall.rbac.vo.AdminPrivilegeVO;
import org.jet.emall.rbac.vo.AdminRoleVO;
import org.jet.emall.rbac.wrapper.RoleIdAndPrivilegeIdListWrapper;
import org.jet.emall.security.comp.DynamicPrivilegeManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-05 20:17
 */
@Api(value = "管理员角色控制器")
@RestController
public class AdminRoleController {


    /**
     *  动态权限管理器，在我们更新了角色的权限后都需要刷新内存中的权限信息
     */

    @Autowired
    private DynamicPrivilegeManager dynamicPrivilegeManager;

    @Autowired
    private CustomAdminRoleService customAdminRoleService;

    @ApiInformation(function = "分页查找角色",menu = "角色管理")
    @ApiOperation("分页查找角色")
    @RequestMapping(value = "/roles",method = RequestMethod.GET)
    public CommonResult<CommonPage<AdminRoleVO>> page(@RequestParam String query, @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        CommonPage<AdminRoleDTO> page = customAdminRoleService.page(query, pageNum, pageSize);
        ((CommonPage<AdminRoleVO>)(Object)page).setList(AdminRoleVoTransfer.INSTANCE.toVoList(page.getList()));
        return CommonResult.success((CommonPage<AdminRoleVO>)(Object)page);
    }

    @ApiInformation(function = "获取所有角色")
    @ApiOperation("获取所有角色")
    @RequestMapping(value = "/roles/all",method = RequestMethod.GET)
    public CommonResult<List<AdminRoleVO>> list(){
        List<AdminRoleDTO> adminRoleDTOList = customAdminRoleService.list();
        List<AdminRoleVO> adminRoleVOList = AdminRoleVoTransfer.INSTANCE.toVoList(adminRoleDTOList);
        return CommonResult.success(adminRoleVOList,"获取所有角色成功");
    }


    @AdminOperationLog(value = "添加角色",logType = LogType.ADD)
    @ApiInformation(function = "添加角色",menu = "角色管理")
    @ApiOperation("添加角色")
    @RequestMapping(value = "/roles",method = RequestMethod.POST)
    public CommonResult<Object> add(@RequestBody NewAdminRoleModel newRole){

        if(ObjectUtils.isAnyNullFields(newRole)){
            return CommonResult.badRequest("新添角色的属性不能为空");
        }
        AdminRoleDTO adminRoleDTO = AdminRoleVoTransfer.INSTANCE.toDto(newRole);
        customAdminRoleService.save(adminRoleDTO);
        return CommonResult.created("添加角色成功");
    }

    @AdminOperationLog(value = "删除角色",logType = LogType.DELETE)
    @ApiInformation(function = "删除角色",menu = "角色管理")
    @ApiOperation("删除角色")
    @RequestMapping(value = "/roles",method = RequestMethod.DELETE)
    public CommonResult<Object> add(@RequestBody List<Integer> ids){
        customAdminRoleService.delete(ids);
        return CommonResult.deleted("删除角色成功");
    }

    @AdminOperationLog(value = "修改角色",logType = LogType.UPDATE)
    @ApiInformation(function = "修改角色",menu = "角色管理")
    @ApiOperation("修改角色")
    @RequestMapping(value = "/roles",method = RequestMethod.PUT)
    public CommonResult<Object> update(@RequestBody UpdateAdminRoleModel updateRole){
        AdminRoleDTO adminRoleDTO = AdminRoleVoTransfer.INSTANCE.toDto(updateRole);
        customAdminRoleService.update(adminRoleDTO);
        return CommonResult.success("修改角色成功");
    }

    @AdminOperationLog(value = "分配权限",logType = LogType.UPDATE)
    @ApiInformation(function = "分配权限",menu = "角色管理")
    @ApiOperation("分配权限")
    @RequestMapping(value = "/roles",method = RequestMethod.PATCH)
    public CommonResult<Object> update(@RequestBody RoleIdAndPrivilegeIdListWrapper wrapper){
        Integer roleId = wrapper.getRoleId();
        List<Integer> privilegeIds = wrapper.getPrivilegeIds();
        customAdminRoleService.update(roleId,privilegeIds);
        //刷新权限信息
        dynamicPrivilegeManager.refresh();
        return CommonResult.success("分配权限成功");
    }

    @ApiInformation(function = "获取角色的权限")
    @ApiOperation("获取角色的权限")
    @RequestMapping(value = "/roles/privileges/{roleId}",method = RequestMethod.GET)
    public CommonResult<List<AdminPrivilegeVO>> listPrivilegesOfRole(@PathVariable Integer roleId){
        List<AdminPrivilegeDTO> list =  customAdminRoleService.listPrivilegesOfRole(roleId);
        List<AdminPrivilegeVO> voList = AdminPrivilegeVoTransfer.INSTANCE.toVoList(list);
        return CommonResult.success(voList,"获取角色的权限");
    }

    @ApiInformation(function = "获取角色的权限ID列表")
    @ApiOperation("获取角色的权限ID列表")
    @RequestMapping(value = "/roles/privileges/ids/{roleId}",method = RequestMethod.GET)
    public CommonResult<List<Integer>> listPrivilegeIdsOfRole(@PathVariable Integer roleId){
        List<Integer> list =  customAdminRoleService.listPrivilegeIdsOfRole(roleId);
        return CommonResult.success(list,"获取角色的权限ID列表成功");
    }

}
